package taggingStrategy;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;



public class TaggingStrategy {
	
	//String folder;
	
	public TaggingStrategy(){
		
	}
	
	public void dynamicLoad() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		
		
		File f=new File("C:\\cs\\pluginFolder\\DateTag.class");// some folder's path like "D:\\myClasses\\"
		
		
		System.out.println("f.isDirectory() = "+f.isDirectory());
		
		File pf = new File(f.getParent());
		
		
		
		URL url=pf.toURI().toURL();
		
		System.out.println("url.getPath() = "+url.getPath());
		
		URL[] urls=new URL[]{url};
		// create a new class loader for this directory
		ClassLoader cl=new URLClassLoader(urls);		
		// load the class file "MyAlgo.class"
		//java.pluginTags.plugins.
		@SuppressWarnings("unchecked")
		Class<PluginTag> c=(Class<PluginTag>)cl.loadClass("plugins.DateTag");
		PluginTag a=c.newInstance();
		a.exmpleMethod();
		
	}
	
	
	/*public class myLoader implements Loader{

		 @SuppressWarnings({ "unchecked", "deprecation" })
		 public Algorithm load(String path) throws Exception {
		  
		  //f stores the full path
		  File f=new File(path);

		  //key<-file's name without '.class' extension 
		  String key=f.getName().split("\\.class")[0];  
		  
		  //load the class
		  ClassLoader cl=new URLClassLoader(new URL[]{new File(f.getParent()).toURL()});
		  Class<Algorithm> c=(Class<Algorithm>) cl.loadClass(key);
		  return c.newInstance();
		 }

		}*/
	
	
	/*	public void addPlugin(String path) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	   {
	           File f=new File(path);// some folder's path like "D:\\myClasses\\"
	           URI uri = f.toURI();
	           
	           java.net.URL url= uri.toURL(); 
	           //url.
	           java.net.URL[] urls= new java.net.URL[]{url};
	           // create a new class loader for this directory
	           ClassLoader cl=new URLClassLoader(urls);
	           // load the class file "MyAlgo.class"
	           String name = f.getName();
	           name = name.substring(0,name.lastIndexOf("."));
	           name= "tagging." + name;
	           try {
	                 copy(path);
	         } catch (IOException e) {
	                 // TODO Auto-generated catch block
	                 e.printStackTrace();
	         }
	        System.out.println(name);
	            Class<tagType> c = (Class<tagType>) cl.loadClass(name);
	            System.out.println(path);
	            String temp = path.substring(path.lastIndexOf('\\')+1, path.length());
	           System.out.println(temp);

	           stype = c.newInstance();
	   
	   }*/
	
	
	
	

}
